package m.jclecai.com.activity.follow;

import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import butterknife.OnTextChanged;
import m.jclecai.com.R;
import m.jclecai.com.adapter.FollowListAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.Response.ResHotPeople;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by 10538 on 2018/3/5.
 */

public class FollowListActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2 {
    List<ResHotPeople.HotPeopleBean> mData;
    FollowListAdapter adapter;
    @BindView(R.id.ptr_listView)
    PullToRefreshListView ptrList;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ivEmpty)
    ImageView ivEmpty;
    //分页
    int page = 1;
    //搜索的文字
    String word;
    //清空数据标识 true清空
    boolean clearFlag = false;

    @Override
    protected int getContentView() {
        return R.layout.activity_follow_list;
    }

    @Override
    protected void initView() {
        super.initView();
        initLoadingLayout(ptrList);
    }

    @Override
    protected void initData() {
        super.initData();
        ptrList.setEmptyView(ivEmpty);
        mData = new ArrayList<>();
        adapter = new FollowListAdapter(mData, this);
        ptrList.setAdapter(adapter);
        ptrList.setOnRefreshListener(this);
        ptrList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //这里我们要进行一个跳转；
                startActivity(HotPeopleInfoActivity.getIntent(FollowListActivity.this, mData.get(position - 1).getUser_id()));
            }
        });
    }

    @OnClick(R.id.tv_search)
    void search() {
        if (etSearch.length() <= 0) {
            finish();
            return;
        }
        showLoadingLayout(ptrList);
        reqeustHotUser();
    }

    @OnTextChanged(R.id.et_search)
    void afterTextChanged(Editable s) {
        Log.e("afterTextChanged", s.toString());
        if (TextUtils.isEmpty(s.toString())) {
            tvSearch.setText(R.string.cancel);
        } else {
            tvSearch.setText(R.string.tv_search);
        }
        clearFlag = true;
        page = 1;
        word = s.toString();
    }

    @OnEditorAction(R.id.et_search)
    boolean searchListener(TextView v, int actionId) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            String s = etSearch.getText().toString();
            if (TextUtils.isEmpty(s)) {
                ToastUtil.showToast(this, R.string.tv_search_hint);
                return false;
            }
            page = 1;
            clearFlag = true;
            word = s;
            showLoadingLayout(ptrList);
            reqeustHotUser();
        }
        return true;
    }

    private void reqeustHotUser() {
        StringBuffer url = new StringBuffer(Constant_Url.HOT_PEOPLE_LIST + "?");
        if (!TextUtils.isEmpty(word))
            url.append("name=" + word + "&");
        url.append("page=" + page);
        HttpManager.getAsyn(url.toString(), new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常!!!");
                ptrList.onRefreshComplete();
                closeLoadingLayout();
            }

            @Override
            public void onResponse(String response) {
                ptrList.onRefreshComplete();
                closeLoadingLayout();
                if (!TextUtils.isEmpty(response)) {
                    if (clearFlag)
                        mData.clear();
                    ResHotPeople resHotPeople = FastOk.get(response, ResHotPeople.class);
                    if (resHotPeople != null && "0".equals(resHotPeople.getStatus())) {
                        if (resHotPeople.getRecords() != null && resHotPeople.getRecords().size() > 0) {
                            page++;
                            mData.addAll(resHotPeople.getRecords());
                        }
                    } else {
                        if (resHotPeople != null) {
                            DisPlay(resHotPeople.getMessage());
                        } else {
                            DisPlay("网络异常");
                        }
                    }
                } else {
                    DisPlay("网络异常!!");
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        clearFlag = true;
        page = 1;
        reqeustHotUser();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        clearFlag = false;
        reqeustHotUser();
    }
}
